import { errLog } from "./log";

/**
 * @param {Object} root 
 * @param {String} propertyName
 * @param {Function | null} getDefault
 */
export function requireDefined(root, propertyName, getDefault=()=>null){
    try{
        let ans=root[propertyName];
        if(ans==undefined){
            return root[propertyName]=getDefault();
        }else{
            return ans;
        }
    }catch(e){
        return root[propertyName]=getDefault();
    }
}
export function tryLog(callback = (...args)=>undefined, ...args){
    try{
        return callback(...args);
    }catch(e){
        errLog(e);
        return undefined;
    }
}

export class Serializable{
    serialize(){
        return {};
    }
    deserialize(_){
        return this;
    }
}
